import { getDetail } from '@/api/labelManagement/tagDataSource';

export const getPrintFormMixin = {
  data() {
    return {
      fieldList: [],
      btnLoading: false,
      printDialog: false,
      otherInfoOption: {
        submitBtn: false,
        emptyBtn: false,
        column: []
      }
    };
  },
  methods: {
    async getDetail(id) {
      try {
        this.btnLoading = true;
        const res = await getDetail(id);
        const data = res.data.data;
        if (data.fieldList) {
          this.fieldList = data.fieldList;
          const column = [];
          data.fieldList.forEach((item) => {
            const obj = {};
            obj.type = item.controlType;
            obj.label = item.name;
            obj.value = item.defaultValue;
            obj.prop = item.field;
            if (item.isRequired) {
              obj.rules = [
                {
                  required: true,
                  message: `请选择${item.name}`
                }
              ];
            }
            if (item.controlType === 'date') {
              obj.format = 'yyyy-MM-dd';
              obj.valueFormat = 'yyyy-MM-dd';
              obj.value = this.$moment(obj.defaultValue).format('YYYY-MM-DD');
            } else if (item.controlType === 'datetime') {
              obj.format = 'yyyy-MM-dd HH:mm:ss';
              obj.valueFormat = 'yyyy-MM-dd HH:mm:ss';
              obj.value = this.$moment(obj.defaultValue).format('yyyy-MM-DD HH:mm:ss');
            } else if (item.controlType === 'select') {
              let data = [];
              if (item.dicData) {
                data = item.dicData.split(',').map((i) => ({ label: i, value: i }));
              }
              obj.dicData = data;
              obj.value = item.defaultValue;
            }
            column.push(obj);
          });
          this.$set(this.otherInfoOption, 'column', column);
        }
      } catch (e) {
        console.warn(e);
      } finally {
        this.btnLoading = false;
      }
    }
  }
};
